<template>
  <div class="permission-container">
    <div>
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="add(1, '0')"
          >添加权限</el-button>
        </div>
        <el-table border row-key="id" :data="list">
          <el-table-column label="菜单名称" prop="name" />
          <el-table-column label="权限标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="success"
                size="small"
                @click="add(2, scope.row.id)"
              >添加</el-button>
              <el-button
                type="primary"
                size="small"
                @click="edit(scope.row)"
              >编辑</el-button>
              <el-button
                type="danger"
                size="small"
                @click="del(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹出层对话框 -->
    <el-dialog :visible.sync="showDialog" title="弹层标题" @close="close">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="100px" :rules="rules" :model="formData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="determine">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import * as _ from 'lodash'
import {
  getPermissionList,
  addPermission,
  delPermission,
  updatePermission
} from '@/api/permisson'
import arrayToTree from 'array-to-tree'

export default {
  name: '',
  data() {
    return {
      isEdit: false,
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.getPermission()
  },
  methods: {
    // 编辑角色
    edit(row) {
      this.isEdit = true
      this.showDialog = true
      this.formData = _.cloneDeep(row)
    },
    // 添加角色
    add(type, pid) {
      this.isEdit = true
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 删除角色
    async del(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delPermission(id)
          this.getPermission() // 获取数据
          this.$notify({
            title: '成功',
            message: '删除角色数据成功!',
            type: 'success',
            offset: 200
          })
        })
        .catch(() => {})
    },
    // 弹出框的确认
    determine() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return this.$message.error('请输入内容')
        this.isEdit ? this.odEdit() : this.doAdd()
      })
    },
    async doAdd() {
      await addPermission(this.formData)
      this.getPermission()
      this.showDialog = false
    },
    async odEdit() {
      await updatePermission(this.formData)
      this.getPermission()
      this.showDialog = false
    },
    // 获取角色数据列表
    async getPermission() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data, {
        parentProperty: 'pid',
        customID: 'id'
      })
    },
    // 弹出框关闭触发事件
    close() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="less" scoped></style>
